<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: options1Index == index }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: options2Index == index }"
            v-for="(item1, index) in options2"
            :key="index"
            @click="options2Fn(index, item1.type)"
            >{{ item1.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <!-- <div v-show="!isReachBottom" class="loading"><img src="@/assets/img/loading.gif" alt=""></div> -->
      <div
        v-show="!isReachBottom"
        style="display: flex; justify-content: center"
      >
        <div class="loading"><i class="iconfont icon-loading"></i></div>
        加载中...
      </div>
      <div v-show="isReachBottom">已经最底部啦~</div>
    </div>
  </div>
</template>                            
<script>
import { GoodsSearchApi } from "@/request/api";

import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  data() {
    return {
      goodsAllList: [],
      // 可视区域，商品先展示8条数据
      goodsListShow: [],
      // 栏目
      did: 0,
      // 实体产品1  虚拟2  全部 0
      min: 0,
      // 最大积分  如果为 0 会忽略其他的逻辑
      max: 0,
      // 关键字
      keyword: "",
      // 当前页数
      page: 1,
      // 显示数量
      size: 8,

      // 排序的数组项
      options1: [
        { min: 0, max: 0, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 分类数组项
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      // 高亮
      options1Index: 0,
      options2Index: 0,

      // 是否加载
      isLoading: false,
      // 是否到底部
      isReachBottom: false,
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    let keyword = this.$route.query.keyword || " ";
    // keyword ||= ""
    this.keyword = keyword;
    this.GoodsSearch();
  },
  mounted() {
    // 监听滚动
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    async GoodsSearch() {
      let res = await GoodsSearchApi({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      console.log(res.data);
      this.goodsAllList = res.data;
      // 1、全部商品（更多），先显示8条数据
      this.goodsListShow = this.goodsAllList.filter(
        (item, index) => index < this.size
      );

      this.page = 1;
      this.isReachBottom = false;
      // isloading初始化
      if (this.goodsAllList.length == this.goodsListShow.length) {
        this.isReachBottom = true;
      }
    },

    options1Fn(idx, min, max) {
      console.log(idx);
      this.options1Index = idx;
      this.min = min;
      this.max = max;
      this.GoodsSearch();
    },
    options2Fn(idx, type) {
      this.options2Index = idx;
      this.type = type;
      this.GoodsSearch();
    },

    // 滚动
    scrollFn() {

      if (getScrollTop() + getClientHeight() > getScrollHeight() - 240) {
        console.log("准备下一页");

        // 情况一：正常情况，将最新的page,size传递给后台，让后台返回对应数据给我们
        // 情况二：积分商城中后台已经把所有数据都给我们了
        // so 从goodsAllList(0~27) 获取  下一页的数据 push到goodsListShow数组中。

        if (this.goodsAllList.length == this.goodsListShow.length) {
          this.isReachBottom = true;
          return;
        }

        // ⭐问题：当鼠标移动过快，会频繁触发，导致page频繁++
        // 优化：先加载完一页，再去加载另一页
        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              // if (this.goodsAllList[i]) {
              //     this.goodsListShow.push(this.goodsAllList[i])
              // }
              // && 如果goodsAllList[i] 有数据(为真) 就执行后面的push
              this.goodsAllList[i] &&
                this.goodsListShow.push(this.goodsAllList[i]);
              console.log(this.goodsAllList[i]);
            }
            this.isLoading = false;
          }, 2000);
        }
      }
    },
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        this.keyword = newVal;
        this.GoodsSearch();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
  /* .loading{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    color: #666;
    img{
      width: 30%;
    }
  } */
  .loading {
    animation: move 2s linear infinite;
    width: 20px;
    height: 20px;
  }

  @keyframes move {
    from {
      transform: rotate(0deg);
    }
    to {
      transform: rotate(360deg);
    }
  }
}
</style>